import { delWo, editWo, getClientPage, doingWo, getWoInfo} from '@/api/workOrder'

const getDefaultState = () => {
  return {
    loading: false,
    woList: [],
    total: 0,
    current: 1,
    size: 10,
    clineWoList: [],
    wait: '',
    doing: '',
    complete: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  }
}

const actions = {
  // 客户分页查询
  async onClientPage({ state }, payload = {}) {
    const { current: newCurrent, size: newSize, ...conditins } = payload
    const newConditions = { ...state.conditins, ...conditins }
    state.loading = true
    const res = await getClientPage({
      current: newCurrent || state.current,
      size: newSize || state.size,
      ...newConditions
    })
    const { retData } = res || {}
    const { records, total, current, size } = retData || {}
    Object.assign(state, {
      loading: false,
      clineWoList: records || [],
      total,
      current,
      size,
      conditins: newConditions
    })
  },
  // 编辑用户
  async onEditWo({ dispatch, commit }, payload = {}) {
    const { success } = await editWo(payload)
    if (success) {
      dispatch('hideModal', {}, { root: true })
      dispatch('onClientPage')
    }
  },
  // 处理工单
  async onDoingWo({ dispatch, rootState }, { code, content, after }) {
    // after()
    // return
    const { success } = await doingWo({ code, content })
    if (success) {
      const { retData } = await getWoInfo(code)
      if (rootState.app) {
        rootState.app.modalData = retData
      }
    }
  },
  // 删除
  async onDelWo({ dispatch }, payload) {
    // const { code } = payload
    const { success } = await delWo(payload)
    if (success) {
      dispatch('onClientPage')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
